package org.appfuse.dao.hibernate;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.appfuse.dao.EducationDAO;
import org.appfuse.model.Education;
import org.hibernate.FlushMode;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Education.
 * @see temp.Education
 * @author MyEclipse - Hibernate Tools
 */
public class EducationDAOHibernate  extends BaseDaoHibernate implements EducationDAO  {

    private static final Log log = LogFactory.getLog(EducationDAOHibernate.class);

	//property constants
	public static final String USERID = "userid";
	public static final String XWLB = "xwlb";
	public static final String XZ = "xz";
	public static final String XXMC = "xxmc";
	public static final String ZYMC = "zymc";
	public static final String ZYLB = "zylb";
	public static final String WCQK = "wcqk";

	public void save(Education transientInstance) {
		log.debug("saving Education instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void update(Education transientInstance) {
		log.debug("saving Education instance");
		try {
			getSession().setFlushMode(FlushMode.AUTO);
			getSession().merge(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

    
	public void delete(Education persistentInstance) {
        log.debug("deleting Education instance");
        try {
        	getSession().setFlushMode(FlushMode.AUTO);
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    

	public Education findById(String a00, Integer id) {
		log.debug("getting Education instance with id: " + id);
		try {
			String queryString = "from Education as model where model.basicInfo.id"
				 + "= ? " + " and model.id = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, a00);
			queryObject.setParameter(1, id);
			return (Education) queryObject.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	

	public Integer newId(String a00){
		log.debug("generate a new id");
		try {
			String queryString = "select max(id) from Education as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		Integer maxId = (Integer) queryObject.uniqueResult();
		if (maxId != null){
			maxId = maxId + 1;
		}else{
			maxId = 1;
		}
		log.debug("generate new id successful");
		return maxId;
			
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}

	public List findByA00(String a00) {
		log.debug("generate a new id");
		try {
			String queryString = " from Education as model where model.basicInfo.id "
				 + "= ?";
		Query queryObject = getSession().createQuery(queryString);
		queryObject.setParameter(0, a00);
		return queryObject.list();
		} catch (RuntimeException re) {
			log.error("generate new id failed", re);
			throw re;
		}
	}
    
    public List findByExample(Education instance) {
        log.debug("finding Education instance by example");
        try {
            List results = getSession()
                    .createCriteria("temp.Education")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding Education instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from Education as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

	public List findByUserid(Object userid) {
		return findByProperty(USERID, userid);
	}
	
	public List findByXwlb(Object xwlb) {
		return findByProperty(XWLB, xwlb);
	}
	
	public List findByXz(Object xz) {
		return findByProperty(XZ, xz);
	}
	
	public List findByXxmc(Object xxmc) {
		return findByProperty(XXMC, xxmc);
	}
	
	public List findByZymc(Object zymc) {
		return findByProperty(ZYMC, zymc);
	}
	
	public List findByZylb(Object zylb) {
		return findByProperty(ZYLB, zylb);
	}
	
	public List findByWcqk(Object wcqk) {
		return findByProperty(WCQK, wcqk);
	}
	
    public Education merge(Education detachedInstance) {
        log.debug("merging Education instance");
        try {
            Education result = (Education) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(Education instance) {
        log.debug("attaching dirty Education instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(Education instance) {
        log.debug("attaching clean Education instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}